import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
import commonJs from "@/tool/common.js";
import { Dialog } from "mand-mobile";

Vue.use(Router);
const index = () => import("./views/index/index.vue");
const commonIndex = () => import("./views/commonIndex/index.vue");
const register = () => import("./views/register/index.vue");
const login = () => import("./views/login/index.vue");
const forgetPsw = () => import("./views/forgetPsw/index.vue");
const resetPsw = () => import("./views/forgetPsw/reset.vue");
const search = () => import("./views/search/index.vue");
// 首页
const home = () => import("./views/home/index.vue");
//平台公告
const platformAnnouncement = () => import("./views/platformAnnouncement/index");
// 商品中心
const goodsCenter = () => import("./views/goodsCenter/index.vue");
const goodsDetail = () => import("./views/goodsCenter/detail.vue");
const goodsComment = () => import("./views/goodsCenter/comment.vue");
// 购物车
const shoppingCart = () => import("./views/shoppingCart/index.vue");
const shoppingOrder = () => import("./views/shoppingCart/order.vue");
const shoppingCartOrder = () => import("./views/shoppingCart/cartOrder.vue");
const shoppingChooseAddress = () =>
  import("./views/shoppingCart/chooseAddress.vue");
const shoppingPay = () => import("./views/shoppingCart/pay.vue");
const shoppingPayMethod = () => import("./views/shoppingCart/payMethod.vue");
const shoppingPaySuccess = () => import("./views/shoppingCart/paySuccess.vue");

// 个人中心
const person = () => import("./views/person/index.vue");
const personAddAddress = () => import("./views/person/addAddress.vue");
const personEditAddress = () => import("./views/person/editAddress.vue");
const personAddCard = () => import("./views/person/addCard.vue");
const personAddress = () => import("./views/person/address.vue");
const personAuthentication = () => import("./views/person/authentication.vue");
const personBalance = () => import("./views/person/balance.vue");
const personBalanceDetail = () => import("./views/person/balanceDetail.vue");
const personCard = () => import("./views/person/card.vue");
const personChangePhone = () => import("./views/person/changePhone.vue");
const personChangeUserName = () => import("./views/person/changeUserName.vue");
const personInfo = () => import("./views/person/info.vue");
const personInvite = () => import("./views/person/invite.vue");
const personMessage = () => import("./views/person/message.vue");
const personResetPayPsw = () => import("./views/person/resetPayPsw.vue");
const personResetLoginPsw = () => import("./views/person/resetLoginPsw.vue");
const personReward = () => import("./views/person/reward.vue");
const personSetPayPsw = () => import("./views/person/setPayPsw.vue");
const personSetting = () => import("./views/person/setting.vue");
const personWithdraw = () => import("./views/person/withdraw.vue");
// 订单
const order = () => import("./views/order/index.vue");
const orderDetail = () => import("./views/order/detail.vue");
const addComment = () => import("./views/order/addComment.vue");
let router = new Router({
  routes: [
    { path: "/", redirect: "/index" },
    {
      path: "/index",
      component: index,
      name: "index",
      meta: { name: "鑫海乐购商城" },
      redirect: "/index/home",
      children: [
        {
          path: "home",
          name: "home",
          component: home,
          meta: {
            name: "首页",
            activeHeaderMenu: "home",
            isShowNav: true
          }
        },
        {
          path: "goods",
          component: commonIndex,
          meta: {
            name: "商品中心",
            isShowNav: true
          },
          children: [
            {
              path: "center",
              name: "goodsCenter",
              component: goodsCenter,
              meta: {
                name: "商品中心",
                activeHeaderMenu: "goodsCenter",
                isShowNav: true
              }
            },
            {
              path: "detail/:id",
              name: "goodsDetail",
              component: goodsDetail,
              meta: {
                name: "商品详情",
                activeHeaderMenu: "goodsCenter",
                isShowNav: false
              }
            },
            {
              path: "comment/:id",
              name: "goodsComment",
              component: goodsComment,
              meta: {
                name: "商品评价",
                activeHeaderMenu: "goodsCenter",
                isShowNav: false
              }
            }
          ]
        },
        {
          path: "shopping",
          component: commonIndex,
          meta: {
            name: "购物",
            isShowNav: true
          },
          children: [
            {
              path: "carts",
              name: "shoppingCart",
              component: shoppingCart,
              meta: {
                name: "购物车",
                activeHeaderMenu: "shoppingCart",
                isShowNav: true,
                needLogin: true
              }
            },
            {
              path: "order/:id",
              name: "shoppingOrder",
              component: shoppingOrder,
              meta: {
                name: "提交订单",
                activeHeaderMenu: "shoppingCart",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "cart-order",
              name: "shoppingCartOrder",
              component: shoppingCartOrder,
              meta: {
                name: "提交订单",
                activeHeaderMenu: "shoppingCart",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "choose-address",
              name: "shoppingChooseAddress",
              component: shoppingChooseAddress,
              meta: {
                name: "选择收货地址",
                activeHeaderMenu: "shoppingCart",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "pay-method",
              name: "shoppingPayMethod",
              component: shoppingPayMethod,
              meta: {
                name: "付款方式",
                activeHeaderMenu: "shoppingCart",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "pay-success",
              name: "shoppingPaySuccess",
              component: shoppingPaySuccess,
              meta: {
                name: "",
                activeHeaderMenu: "shoppingCart",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "pay",
              name: "shoppingPay",
              component: shoppingPay,
              meta: {
                name: "快捷支付",
                activeHeaderMenu: "shoppingCart",
                isShowNav: false,
                needLogin: true
              }
            }
          ]
        },
        {
          path: "person",
          component: commonIndex,
          meta: {
            name: "个人中心",
            isShowNav: true
          },
          children: [
            {
              path: "center",
              name: "person",
              component: person,
              meta: {
                name: "个人中心",
                activeHeaderMenu: "person",
                isShowNav: true,
                needLogin: true
              }
            },
            {
              path: "add-address",
              name: "personAddAddress",
              component: personAddAddress,
              meta: {
                name: "添加收货地址",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "edit-address/:id",
              name: "personEditAddress",
              component: personEditAddress,
              meta: {
                name: "修改收货地址",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "add-card",
              name: "personAddCard",
              component: personAddCard,
              meta: {
                name: "新增银行卡",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "address",
              name: "personAddress",
              component: personAddress,
              meta: {
                name: "收货地址",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "authentication",
              name: "personAuthentication",
              component: personAuthentication,
              meta: {
                name: "实名认证",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "balance",
              name: "personBalance",
              component: personBalance,
              meta: {
                name: "余额",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "balance-detail",
              name: "personBalanceDetail",
              component: personBalanceDetail,
              meta: {
                name: "明细",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "card",
              name: "personCard",
              component: personCard,
              meta: {
                name: "银行卡管理",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "change-phone",
              name: "personChangePhone",
              component: personChangePhone,
              meta: {
                name: "修改绑定手机号",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "change-user-name",
              name: "personChangeUserName",
              component: personChangeUserName,
              meta: {
                name: "修改用户名",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "info",
              name: "personInfo",
              component: personInfo,
              meta: {
                name: "个人信息",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "invite",
              name: "personInvite",
              component: personInvite,
              meta: {
                name: "邀请好友",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "message",
              name: "personMessage",
              component: personMessage,
              meta: {
                name: "消息通知",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "reset-login-psw",
              name: "personResetLoginPsw",
              component: personResetLoginPsw,
              meta: {
                name: "修改登录密码",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "reset-pay-psw",
              name: "personResetPayPsw",
              component: personResetPayPsw,
              meta: {
                name: "重置支付密码",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "reward",
              name: "personReward",
              component: personReward,
              meta: {
                name: "我的奖励",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "set-pay-psw",
              name: "personSetPayPsw",
              component: personSetPayPsw,
              meta: {
                name: "设置支付密码",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "setting",
              name: "personSetting",
              component: personSetting,
              meta: {
                name: "安全中心",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "withdraw",
              name: "personWithdraw",
              component: personWithdraw,
              meta: {
                name: "提现",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true,
                needCard: true,
                needSetPayPsw: true
              }
            }
          ]
        },
        {
          path: "order",
          component: commonIndex,
          meta: {
            name: "我的订单",
            isShowNav: true
          },
          children: [
            {
              path: "index",
              name: "order",
              component: order,
              meta: {
                name: "我的订单",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "detail/:id",
              name: "orderDetail",
              component: orderDetail,
              meta: {
                name: "订单详情",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            },
            {
              path: "add-comment/:id",
              name: "addComment",
              component: addComment,
              meta: {
                name: "评价",
                activeHeaderMenu: "person",
                isShowNav: false,
                needLogin: true
              }
            }
          ]
        },
        {
          path: "/register",
          name: "register",
          component: register,
          meta: { name: "注册" }
        },
        {
          path: "/login",
          name: "login",
          component: login,
          meta: { name: "登录" }
        },
        {
          path: "/forget",
          name: "forgetPsw",
          component: forgetPsw,
          meta: { name: "找回密码" }
        },
        {
          path: "/reset/:id",
          name: "resetPsw",
          component: resetPsw,
          meta: { name: "重置密码" }
        },
        {
          path: "/platform-announcement/:id",
          name: "platformAnnouncement",
          component: platformAnnouncement,
          meta: {
            name: "平台公告",
            activeHeaderMenu: "platformAnnouncement",
            isShowNav: false,
            needLogin: true
          }
        }
      ]
    },
    {
      path: "/search",
      name: "search",
      component: search,
      meta: { name: "搜索" }
    }
  ]
});
export default router;
router.beforeEach(function(to, from, next) {
  let validate = true;
  let readyPromise = null;
  let readyPromiseRS = null;
  readyPromise = new Promise(resolve => {
    readyPromiseRS = resolve;
  });
  if (to.matched[0].name === "index") {
    // 只在刷新的时候获取sessionStorage的state
    if (from.matched.length === 0) {
      let state = window.sessionStorage.getItem("state");
      if (state) {
        store.replaceState(JSON.parse(state));
      }
      commonJs.getInfo(() => {
        readyPromiseRS();
      });
    } else {
      readyPromiseRS();
    }
  } else {
    readyPromiseRS();
  }
  readyPromise.then(() => {
    if (to.meta.needLogin) {
      if (!store.state.isLogin) {
        validate = false;
        Dialog.confirm({
          title: "未登录",
          content: "亲爱的用户：您尚未进行登录，请前去登录",
          confirmText: "去登录",
          onConfirm: () => {
            router.push({ name: "login" });
          }
        });
      } else {
        if (to.meta.needCard) {
          if (!store.state.isCard) {
            validate = false;
            Dialog.confirm({
              title: "添加提现银行卡",
              content: "亲爱的用户：您尚未添加提现银行卡，请前往添加",
              confirmText: "去添加",
              onConfirm: () => {
                router.push({ name: "personAddCard" });
              }
            });
          } else {
            if (to.meta.needSetPayPsw) {
              if (!store.state.isSetPayPsw) {
                validate = false;
                Dialog.confirm({
                  title: "设置支付密码",
                  content: "亲爱的用户：您尚未设置支付密码，请前往设置",
                  confirmText: "去设置",
                  onConfirm: () => {
                    router.push({ name: "personSetPayPsw" });
                  }
                });
              }
            }
          }
        }
      }
    }
    if (validate) {
      if (to.meta.isShowNav) {
        store.dispatch("changeIsShowNav", to.meta.isShowNav);
      } else {
        store.dispatch("changeIsShowNav", false);
      }
      store.dispatch("changeActiveMenu", to.meta.activeHeaderMenu);
      store.dispatch("changeCurrentRouterName", to.meta.name);
    }
    if (validate) {
      next();
    }
  });
});
